package javawdw.picture.core;

import javawdw.picture.api.Picture;

/**
 * 控制绘制区域，标识图片的可绘制区域
 * 
 * @author David
 * 
 */
public class ShapeZone {
	private Position leftTop;
	private Position rightBottom;

	/**
	 * 根据两个坐标构造长方形绘制区域
	 * 
	 * @param leftTop
	 *            左上坐标点
	 * @param rightBottom
	 *            右下坐标点
	 */
	public ShapeZone(Position leftTop, Position rightBottom) {
		this.leftTop = leftTop;
		this.rightBottom = rightBottom;
	}

	/**
	 * 根据两个坐标构造长方形绘制区域
	 * 
	 * @param leftTopX
	 *            左上x坐标
	 * @param leftTopY
	 *            左上y坐标
	 * @param rightBottomX
	 *            右下x坐标
	 * @param rightBottomY
	 *            右下y坐标
	 */
	public ShapeZone(int leftTopX, int leftTopY, int rightBottomX,
			int rightBottomY) {
		this.leftTop = new Position(leftTopX, leftTopY);
		this.rightBottom = new Position(rightBottomX, rightBottomY);
	}

	/**
	 * 根据图片获得图片的默认绘制区域，即整张图片都可绘制
	 * 
	 * @param picture
	 *            目标图片
	 * @return 返回默认绘制区域
	 */
	public static ShapeZone getDefaultDrawZone(Picture picture) {
		Position leftTop = new Position(0, 0);
		Position rightBottom = new Position(picture.getWidth(), picture
				.getHeight());
		return new ShapeZone(leftTop, rightBottom);
	}

	/**
	 * 根据图片和给定空闲像素获得绘制区域，即在图片去掉rest大小的范围为绘制区域
	 * 
	 * @param picture
	 *            目标图片
	 * @param rest
	 *            给定空闲像素，上下左右都为rest
	 * @return 返回得到的绘制区域
	 */
	public static ShapeZone getDrawZone(Picture picture, int rest) {
		Position leftTop = new Position(rest, rest);
		Position rightBottom = new Position(picture.getWidth() - rest, picture
				.getHeight()
				- rest);
		return new ShapeZone(leftTop, rightBottom);
	}

	/**
	 * 获得左上坐标
	 * 
	 * @return 返回左上坐标
	 */
	public Position getLeftTop() {
		return leftTop;
	}

	/**
	 * 设置左上坐标
	 * 
	 * @param leftTop
	 *            左上坐标
	 */
	public void setLeftTop(Position leftTop) {
		this.leftTop = leftTop;
	}

	/**
	 * 获得右下坐标
	 * 
	 * @return 返回右下坐标
	 */
	public Position getRightBottom() {
		return rightBottom;
	}

	/**
	 * 设置右下坐标
	 * 
	 * @param rightBottom
	 *            右下坐标
	 */
	public void setRightBottom(Position rightBottom) {
		this.rightBottom = rightBottom;
	}

	/**
	 * 获得绘制区域宽度
	 * 
	 * @return 返回宽度
	 */
	public int getWidth() {
		return this.rightBottom.getX() - this.leftTop.getX();
	}

	/**
	 * 获得绘制区域高度
	 * 
	 * @return 返回高度
	 */
	public int getHeight() {
		return this.rightBottom.getY() - this.leftTop.getY();
	}
}